<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>破天星辰科技网络-新闻滚动字幕系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background-color: transparent;
            font-family: Arial, sans-serif;
            overflow: hidden;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        /* 滚动容器 */
        .ticker-container {
            position: relative;
            width: 100%;
            height: 60px; /* 滚动条高度 */
            overflow: hidden;
            background-color: transparent;
        }
        
        /* 滚动内容 */
        .ticker-content {
            position: absolute;
            top: 0;
            left: 100%; /* 从右侧开始 */
            white-space: nowrap;
            display: flex;
            align-items: center;
            height: 100%;
        }
        
        /* 新闻项 */
        .news-item {
            display: inline-flex;
            align-items: center;
            margin: 0 40px;
            color: black;
            font-size: 24px;
            font-weight: bold;
        }
        
        /* 台标 */
        .station-logo {
            height: 36px; /* 与字体高度匹配 */
            width: auto;
            margin-right: 15px;
        }
        
        /* 分隔符 */
        .news-separator {
            margin: 0 20px;
            font-size: 24px;
            color: black;
        }
        
        /* 控制器 */
        .controls {
            position: fixed;
            bottom: 30px;
            left: 0;
            right: 0;
            text-align: center;
            background-color: rgba(255, 255, 255, 0.8);
            padding: 15px;
            border-radius: 10px;
            max-width: 600px;
            margin: 0 auto;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }
        
        .control-buttons {
            margin-bottom: 15px;
        }
        
        button {
            padding: 8px 15px;
            margin: 0 5px;
            border: none;
            border-radius: 4px;
            background-color: #333;
            color: white;
            cursor: pointer;
            font-size: 14px;
        }
        
        button:hover {
            background-color: #555;
        }
        
        .speed-control {
            width: 80%;
            max-width: 400px;
            margin: 0 auto;
        }
        
        .speed-labels {
            display: flex;
            justify-content: space-between;
            margin-top: 5px;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <!-- 滚动容器 -->
    <div class="ticker-container">
        <div class="ticker-content" id="tickerContent">
            <div class="loading">正在加载新闻...</div>
        </div>
    </div>

    <!-- 控制器 -->
    <div class="controls">
        <div class="control-buttons">
            <button id="pauseBtn">暂停滚动</button>
            <button id="resumeBtn">继续滚动</button>
            <button id="refreshBtn">刷新新闻</button>
            <button id="toggleLoopBtn">关闭循环</button>
        </div>
        
        <div class="speed-control">
            <input type="range" id="speedSlider" min="0.3" max="10" step="0.1" value="1">
            <div class="speed-labels">
                <span>0.3x</span>
                <span id="currentSpeed">1.0x</span>
                <span>10x</span>
            </div>
        </div>
    </div>

    <script>
        // 配置变量
        let currentSpeedFactor = 1;
        let isPaused = false;
        let isLooping = true;
        let newsData = [];
        let animationId = null;
        
        // 获取新闻数据并处理
        async function fetchNews() {
            try {
                const response = await fetch('https://api.mhimg.cn/api/Daily_news');
                
                // 检查响应状态
                if (!response.ok) {
                    throw new Error(`API请求失败: ${response.status} ${response.statusText}`);
                }
                
                const text = await response.text();
                
                // 处理API返回的文本数据
                newsData = processNewsText(text);
                
                // 更新DOM
                displayNews(newsData);
            } catch (error) {
                console.error('获取新闻失败:', error);
                const tickerContent = document.getElementById('tickerContent');
                tickerContent.innerHTML = `<div class="news-item">新闻加载失败: ${error.message}</div>`;
            }
        }

        // 处理API返回的文本数据 - 简化版本
        function processNewsText(text) {
            const newsItems = [];
            // 按分隔符分割新闻
            const sections = text.split('--------------');
            
            for (const section of sections) {
                if (!section.trim()) continue;
                
                let title = '';
                let content = '';
                
                // 查找标题和内容的位置
                const titleIndex = section.indexOf('标题：');
                const contentIndex = section.indexOf('内容：');
                
                if (titleIndex !== -1) {
                    const titleEnd = contentIndex !== -1 ? contentIndex : section.length;
                    title = section.substring(titleIndex + 3, titleEnd).trim();
                }
                
                if (contentIndex !== -1) {
                    content = section.substring(contentIndex + 3).trim();
                }
                
                // 如果标题或内容有值，就加入新闻数组
                if (title || content) {
                    newsItems.push({
                        title: title,
                        content: content
                    });
                }
            }
            
            return newsItems;
        }

        // 显示新闻
        function displayNews(newsItems) {
            const tickerContent = document.getElementById('tickerContent');
            
            if (newsItems.length === 0) {
                tickerContent.innerHTML = '<div class="news-item">今日暂无新闻</div>';
                return;
            }
            
            // 创建新闻HTML
            let newsHTML = '';
            const separator = '<span class="news-separator"> ★ </span>';
            
            // 添加台标
            newsHTML += '<img src="./xwlogo/ptxw.png" class="station-logo" alt="台标">';
            
            newsItems.forEach((news, index) => {
                // 显示标题和内容（如果存在）
                let itemHTML = '';
                if (news.title) {
                    itemHTML += `<span>${news.title}</span>`;
                }
                if (news.content) {
                    itemHTML += `<span>${news.content}</span>`;
                }
                
                newsHTML += `<div class="news-item">${itemHTML}</div>`;
                
                if (index < newsItems.length - 1) {
                    newsHTML += separator;
                }
            });
            
            // 复制一份内容以实现无缝滚动
            tickerContent.innerHTML = newsHTML + newsHTML;
            
            // 开始滚动动画
            startAnimation();
        }

        // 开始滚动动画
        function startAnimation() {
            const tickerContent = document.getElementById('tickerContent');
            const containerWidth = document.querySelector('.ticker-container').offsetWidth;
            const contentWidth = tickerContent.scrollWidth / 2; // 因为有两份相同内容
            
            // 计算速度
            const baseSpeed = 100; // 基础速度（像素/秒）
            const speed = baseSpeed * currentSpeedFactor;
            
            // 清除之前的动画
            if (animationId) {
                cancelAnimationFrame(animationId);
            }
            
            // 初始位置
            let position = 0;
            
            // 动画函数
            function animate() {
                if (!isPaused) {
                    // 更新位置
                    position -= speed / 60; // 60fps
                    
                    // 当内容完全移出屏幕时，重置位置
                    if (position <= -contentWidth) {
                        position = 0;
                    }
                    
                    // 应用位置
                    tickerContent.style.transform = `translateX(${position}px)`;
                }
                
                // 继续动画
                animationId = requestAnimationFrame(animate);
            }
            
            // 开始动画
            animate();
        }

        // 初始化事件监听
        function initEventListeners() {
            // 速度滑块
            const speedSlider = document.getElementById('speedSlider');
            speedSlider.addEventListener('input', function() {
                currentSpeedFactor = parseFloat(this.value);
                document.getElementById('currentSpeed').textContent = currentSpeedFactor.toFixed(1) + 'x';
            });
            
            // 暂停按钮
            document.getElementById('pauseBtn').addEventListener('click', function() {
                isPaused = true;
            });
            
            // 继续按钮
            document.getElementById('resumeBtn').addEventListener('click', function() {
                isPaused = false;
            });
            
            // 刷新按钮
            document.getElementById('refreshBtn').addEventListener('click', function() {
                if (animationId) {
                    cancelAnimationFrame(animationId);
                }
                document.getElementById('tickerContent').innerHTML = '<div class="loading">正在加载新闻...</div>';
                fetchNews();
            });
            
            // 循环切换按钮
            document.getElementById('toggleLoopBtn').addEventListener('click', function() {
                isLooping = !isLooping;
                this.textContent = isLooping ? '关闭循环' : '开启循环';
            });
        }

        // 页面加载时初始化
        document.addEventListener('DOMContentLoaded', function() {
            fetchNews();
            initEventListeners();
        });
        
        // 每30分钟取接口刷新下新闻
        setInterval(fetchNews, 30 * 60 * 1000);

        //逻辑部分完成 破天星辰科技网络
    </script>
</body>
</html>